﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WmsService.Generate
{
    /// <summary>
    /// 工厂方法模式--号码管理对象基类
    /// </summary>
    public abstract class RowIDControllerBase
    {
        private int currentID = -1;
        private const string QueryString =@"select isnull(MAX({0}),0)  as RowID from dbo.{1} where 1=1 {2}";
        private string queryExecute = String.Empty;

        protected abstract string GetTableName();
        protected abstract string GetFieldName();
        protected virtual string GetCondition()
        {
            return " and 1=1";
        }

        public int GetNextRowID()
        {
            queryExecute = String.Format(QueryString, GetFieldName(), GetTableName(), GetCondition());
            if(currentID <0)//未执行查询
            {
                var ctx = Factory.ContextFactory.Create();
                currentID = ctx.ExecuteStoreQuery<int>(queryExecute).FirstOrDefault();
                currentID++;
                return currentID;
            }
            else
            {
                currentID++;
                return currentID;
            }
        }
    }

}